package com.rpoonolly.simplegame.textures;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import javax.imageio.ImageIO;

public class TextureTileMap implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private int mapWidth, mapHeight, imgTileWidth, imgTileHeight;
	private BufferedImage[][] imgMap;
	
	public TextureTileMap(TextureFileDescriptor textureDef) {
		try {
			BufferedImage mainImg = ImageIO.read(new File(textureDef.filename));
			
			this.mapWidth = textureDef.mapWidth;
			this.mapHeight = textureDef.mapHeight;
			
			imgTileWidth = mainImg.getWidth() / mapWidth;
			imgTileHeight = mainImg.getHeight() / mapHeight;
			
			this.imgMap = new BufferedImage[mapWidth][mapHeight];
			
			for(int x = 0; x < mapWidth; x++) {
				for(int y = 0; y < mapHeight; y++) {
					this.imgMap[x][y] = mainImg.getSubimage(x * imgTileWidth, y * imgTileHeight, imgTileWidth, imgTileHeight);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	
	public BufferedImage getTextureTile(int x, int y) {
		return imgMap[x][y];
	}
}
